﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class OperationBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IOperationDA OperationDA = DataAccess.CreateOperationDA();
        private PubParasInfo pubParasInfo;

        public OperationBLL()
        {
            this.pubParasInfo = null;
        }

        public OperationBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public OperationBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<OperationInfo> operationInfos = new List<OperationInfo>();
                OperationInfo operationInfo = null;
                foreach (string id in IDS)
                {
                    operationInfo = OperationDA.GetOperationByID(dataBase, tran, id, this.dataBaseID[0]);
                    operationInfos.Add(operationInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (OperationDA.DeleteOperation(dataBase, tran, IDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<OperationInfo>(dataBase, tran, operationInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "OPERATION", this.dataBaseID);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return OperationDA.GetAllOperation(this.dataBaseID[0]);
        }

        public OperationInfo GetInfoByID(string operationid)
        {
            return OperationDA.GetOperationByID(operationid, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(OperationQueryEntity operationQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(operationQuery.OperationID))
            {
                sqlWhere.Add(" OPERATION_ID " + operationQuery.OperationID);
            }
            if (!string.IsNullOrEmpty(operationQuery.Descr))
            {
                sqlWhere.Add(" DESCR " + operationQuery.Descr);
            }
            if (!string.IsNullOrEmpty(operationQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + operationQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(operationQuery.OperationType))
            {
                sqlWhere.Add(" OPERATION_TYPE " + operationQuery.OperationType);
            }
            return OperationDA.GetOperationByQueryList(sqlWhere, operationQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            string operationid = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                OperationInfo operation = obj as OperationInfo;
                if (OperationDA.CheckOperationIDUnique(dataBase, tran, operation.OperationID, this.dataBaseID[0]))
                {
                    throw new Exception("OperationID:" + operation.OperationID + "已经存在,请重新输入!");
                }
                operation.UpdatedBy = operation.CreatedBy;
                operation.CreatedDate = DateTime.Now;
                operation.UpdatedDate = DateTime.Now;
                if (OperationDA.InsertOperation(dataBase, tran, operation, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<OperationInfo>(dataBase, tran, null, operation, AuditAction.Insert, this.pubParasInfo.UserID, "OPERATION", this.dataBaseID);
                    operationid = operation.OperationID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return operationid;
        }

        public string Update(object obj)
        {
            string operationid = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                OperationInfo operation = obj as OperationInfo;
                operation.UpdatedDate = DateTime.Now;
                OperationInfo oldOperationInfo = OperationDA.GetOperationByID(dataBase, tran, operation.OperationID, this.dataBaseID[0]);
                if (OperationDA.UpdateOperation(dataBase, tran, operation, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<OperationInfo>(dataBase, tran, oldOperationInfo, operation, AuditAction.Update, this.pubParasInfo.UserID, "OPERATION", this.dataBaseID);
                    operationid = operation.OperationID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return operationid;
        }
    }
}

